package k8055;


import java.io.File;
import java.io.IOException;

import com.sun.jna.IntegerType;
import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.NativeLongByReference;

import k8055.JWrapperK8055;


public class JNA_DirectMapped extends JWrapperK8055
{
	
	static
	{
		Native.setProtected(true);
		//Native.register("K8055D");
	}
	

	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @param library_K8055D the File of the library K8055D
	 * @since 02.01.2010
	 * @throws IOException
	 */
	public JNA_DirectMapped(File library_K8055D) throws IOException
	{
		Native.register(library_K8055D.getCanonicalPath());
	}
	
	/* (non-Javadoc)
	 * @see k8055.JWrapperK8055#searchDevices0()
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long searchDevices0()
	{
		return SearchDevices().longValue();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#closeDevice0()
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void closeDevice0()
	{
		CloseDevice();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#openDevice0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long openDevice0(long address)
	{
		return OpenDevice(address).longValue();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#readAnalogChannel0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long readAnalogChannel0(long channelNo)
	{
		return ReadAnalogChannel(channelNo + 1).longValue();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#readAllAnalog0()
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long[] readAllAnalog0()
	{
		NativeLongByReference[] i = {new NativeLongByReference(), new NativeLongByReference()};
		ReadAllAnalog(i[0], i[1]);
		long[] a = new long[2];
		a[0] = i[0].getValue().longValue();
		a[1] = i[1].getValue().longValue();
		return a;
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setAnalogChannel0(long, long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void setAnalogChannel0(long channelNo, long data)
	{
		OutputAnalogChannel(new NativeLong(channelNo), new NativeLong(data));
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setPWM(long, long, long)
	 * 
	 * @author TTR6214
	 * @since 14.01.2013
	 */
	@Override
	protected void setPWMChannel0(long channelNo, long data, long frequency)
	{
		SetPWM(new NativeLong(channelNo), new NativeLong(data), new NativeLong(frequency));
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setAllAnalogOutput0(long, long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void setAllAnalogOutput0(long data1, long data2)
	{
		OutputAllAnalog(new NativeLong(data1), new NativeLong(data2));
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setAllDigital0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void setAllDigital0(long data)
	{
		WriteAllDigital(data);
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setDigitalChannel0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void setDigitalChannel0(long channelNo)
	{
		SetDigitalChannel(channelNo);
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#clearDigitalChannel0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void clearDigitalChannel0(long channelNo)
	{
		ClearDigitalChannel(channelNo);
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#readDigitalChannel0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected boolean readDigitalChannel0(long channel)
	{
		return ReadDigitalChannel(channel);
	}
	
	@Override
	protected int readBackDigitalOut()
	{
		return ReadBackDigitalOut();
	}
	
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#readAllDigital0()
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long readAllDigital0()
	{
		return ReadAllDigital().longValue();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#readCounter0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long readCounter0(long channelNo)
	{
		return ReadCounter(channelNo);
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#resetCounter0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void resetCounter0(long channelNo)
	{
		ResetCounter(channelNo);
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setCounterDebounceTime0(long, long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void setCounterDebounceTime0(long channelNo, long debounceTimeMillis)
	{
		SetCounterDebounceTime(new NativeLong(channelNo), new NativeLong(debounceTimeMillis));
	}
	
	/*
	 * (non-Javadoc)
	 * k8055.JWrapperK8055#setCurrentDevice0(long)
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected long setCurrentDevice0(long address)
	{
		return SetCurrentDevice(address).longValue();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#clearAllDigital0()
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void clearAllDigital0()
	{
		ClearAllDigital();
	}
	
	/*
	 * (non-Javadoc)
	 * @k8055.JWrapperK8055#setAllDigital0()
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 02.01.2010
	 */
	@Override
	protected void setAllDigital0()
	{
		SetAllDigital();
	}
	
	/**
	 * This method connects the driver to a board with address {@code cardAddress}.
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 13.12.2009
	 * @param cardAddress
	 * @return the address of the connected card, if connected successful<br>
	 *         -1 if connecting failed because<br>
	 *         - the driver is not available<br>
	 *         - the card with address {@code cardAddress} can not be found<br>
	 *         - the driver is already connected to a card
	 */
	protected native static NativeLong OpenDevice(long cardAddress);
	
	/**
	 * Tries to close the connection to the card. Disconnecting the card will effect that only
	 * {@link #OpenDevice(long)} and {@link #SearchDevices()} will work.
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 13.12.2009
	 */
	protected native static void CloseDevice();
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 12.12.2009
	 * @param channelNo
	 * @return a value from 0 to 255, 8Bit
	 */
	protected native static NativeLong ReadAnalogChannel(long channelNo);
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 13.12.2009
	 * @param p1 {@link Pointer} to a {@link NativeLong}, the value of channel1 will be written
	 *        into it.
	 * @param p2 {@link Pointer} to a {@link NativeLong}, the value of channel2 will be written
	 *        into it.
	 */
	protected native static void ReadAllAnalog(NativeLongByReference p1, NativeLongByReference p2);
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 13.12.2009
	 * @param channelNo Number, 1 or 2
	 * @param value new 8Bit-coded value from 0 to 255
	 */
	protected native static void OutputAnalogChannel(NativeLong channelNo, NativeLong value);
	
	/**
	 * @author TTR6214
	 * @since 14.01.2013
	 * @param channelNo Number, 1 or 2
	 * @param value new 8Bit-coded value from 0 to 255
	 * @param frequence ?
	 */
	
	protected native static void SetPWM(NativeLong channelNo, NativeLong value, NativeLong frequence);
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 13.12.2009
	 * @param value1 new 8Bit-coded value from 0 to 255 for channel 1
	 * @param value2 new 8Bit-coded value from 0 to 255 for channel 2
	 */
	protected native static void OutputAllAnalog(NativeLong value1, NativeLong value2);
	
	/*//*
	 * @author genodeftest (Christian Stadelmann)
	 * @param channelNo 1 or 2 for the specific channel
	 * @since 11.12.2009
	 *
	private native static void ClearAnalogChannel(long channelNo);*/

	protected native static void ClearAllAnalog();
	
	/*//*
	 * @author genodeftest (Christian Stadelmann)
	 * @param channelNo 1 or 2 for the specific channel
	 * @since 11.12.2009
	 *
	private native static void SetAnalogChannel(long channelNo);*/

	protected native static void SetAllAnalog();
	
	/**
	 * Set all digital outputs (1 = ON, 0 = OFF)<br>
	 * Channel 1: x*1<br>
	 * Channel 2: x*2<br>
	 * Channel 3: x*4<br>
	 * Channel 4: x*8<br>
	 * Channel 5: x*16<br>
	 * Channel 6: x*32<br>
	 * Channel 7: x*64<br>
	 * Channel 8: x*128<br>
	 * eg. data = 7 : ON-ON-ON-OFF-OFF-OFF-OFF-OFF
	 * 
	 * @param data
	 */
	protected native static void WriteAllDigital(long data);
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 11.12.2009
	 * @param channelNo a channel number from 1 to 8
	 */
	protected native static void ClearDigitalChannel(long channelNo);
	
	protected native static void ClearAllDigital();
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 11.12.2009
	 * @param channelNo a channel number from 1 to 8
	 */
	protected native static void SetDigitalChannel(long channelNo);
	
	protected native static void SetAllDigital();
	
	/**
	 * @author TTR6214
	 * @since 25.12.2012
	 * @param channelNo
	 * @return 
	 * Read all digital outputs <br>
	 * Channel 1: 1<br>
	 * Channel 2: 2<br>
	 * Channel 3: 4<br>
	 * Channel 4: 8<br>
	 * Channel 5: 16<br>
	 * Channel 6: 32<br>
	 * Channel 7: 64<br>
	 * Channel 8: 128<br>
	 * ex: ON-ON-ON-OFF-OFF-OFF-OFF-OFF, int = 7 (1+2+4)
	 * 	   ON-OFF-ON-OFF-OFF-OFF-OFF-ON, int = 133 (1+4+128)
	 */
	
	protected native static int ReadBackDigitalOut();
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 11.12.2009
	 * @param channelNo
	 * @return true if channel is ON<br>
	 *         false if channel is OFF
	 */
	protected native static boolean ReadDigitalChannel(long channelNo);
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @since 12.12.2009
	 * @return bitwise coded:<br>
	 *         ON = 1, OFF = 0;<br>
	 *         <table border="1">
	 *         <tr>
	 *         <th>Input channel</th>
	 *         <th>multiplicator</th>
	 *         </tr>
	 *         <tr>
	 *         <td>1</td>
	 *         <td>1 = 2<sup>0</sup></td>
	 *         </tr>
	 *         <tr>
	 *         <td>2</td>
	 *         <td>2 = 2<sup>1</sup></td>
	 *         </tr>
	 *         <tr>
	 *         <td>3</td>
	 *         <td>4 = 2<sup>3</sup></td>
	 *         </tr>
	 *         <tr>
	 *         <td>4</td>
	 *         <td>8 = 2<sup>4</sup></td>
	 *         </tr>
	 *         <tr>
	 *         <td>5</td>
	 *         <td>16 = 2<sup>5</sup></td>
	 *         </tr>
	 *         </table>
	 */
	protected native static NativeLong ReadAllDigital();// C return type: long
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @param channelNo 1 or 2 for the specific channel
	 * @return the current value
	 * @since 11.12.2009
	 */
	protected native static long ReadCounter(long channelNo);
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @param channelNo 1 or 2 for the specific channel
	 * @since 11.12.2009
	 */
	protected native static void ResetCounter(long channelNo);
	
	/**
	 * @author genodeftest (Christian Stadelmann)
	 * @param channelNo 1 or 2 for the specific channel
	 * @param debounceTime the new debounce time// TODO min/max!!
	 * @since 11.12.2009
	 */
	protected native static void SetCounterDebounceTime(NativeLong channelNo,
		NativeLong debounceTime);// C arguments: long, long
	
	//@Deprecated 
	//Does not work with Java!!
	//void Version();
	
	/**
	 * @return Bitcode of all connected Boards:<br>
	 *         Address 0 : 2<sup>0</sup> = 1<br>
	 *         Address 1 : 2<sup>1</sup> = 2<br>
	 *         Address 2 : 2<sup>2</sup> = 4<br>
	 *         Address 3 : 2<sup>3</sup> = 8<br>
	 *         e.g.: Board 0 and 2 are connected, so SearchDevices() will return 5 (=1+4)
	 */
	protected native static NativeLong SearchDevices();// C return type: long
	
	/**
	 * Tries to change the current device to the one with the new address. If connecting failed
	 * the previous connected device stays connected. Connecting failure will NOT cause the
	 * current card to be disconnected.
	 * 
	 * @author genodeftest (Christian Stadelmann)
	 * @since 13.12.2009
	 * @param cardAdress the new device address
	 * @return -1 if connecting failed, lngCardAddress if connecting succeeded
	 */
	protected native static NativeLong SetCurrentDevice(long cardAdress);
	
	public JNA_DirectMapped() {
		
		
	}
}